package vaani.internal.applicationmanagers;

import java.io.IOException;
import java.util.Vector;
import vaani.internal.ProcessInstance;
import vaani.internal.ProcessInstanceBash;
import vaani.internal.beagle.Result;
import vaani.shabd.ActivityMiner;
import vaani.shabd.ApplicationMiner;
import vaani.shabd.Locator;

/**
 *
 * @author sourcemorph
 */
public abstract class GenericManagerBashType implements ApplicationManager {

    private Vector<ProcessInstance> instances;

    public GenericManagerBashType(Vector<String> arguments) {
        /*high level search to find the url of files related to the arguments*/
        Vector<Result> searchResults = Locator.find(arguments);
        if(searchResults.size() != 0) {
            instances = new Vector<ProcessInstance>();
            for(Result r : searchResults) {
                String defApp = ApplicationMiner.MatchApplication(r.getFileType());
                if(ActivityMiner.getActivityApplication(1).contains(defApp)) {
                    instances.add(createProcessInstance(r));
                }
            }
        }
    }

    @Override
    public Vector<ProcessInstance> getProcessInstance() {
        return instances;
    }

    protected abstract ProcessInstance createProcessInstance(Result r);

    @Override
    public boolean runProcess(int x) {
        try {
            Runtime.getRuntime().exec(((ProcessInstanceBash)instances.get(x)).getCommand());
        } catch(IOException ex) {
            ex.printStackTrace();
            return false;
        }
        return true;
    }

    @Override
    public boolean runProcess() {
        return runProcess(0);
    }
}
